package ch.fhnw.qualiscan.application.controllers;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.models.AdminModel;
import ch.fhnw.qualiscan.application.models.DomainModel;
import ch.fhnw.qualiscan.application.models.QuestionModel;
import ch.fhnw.qualiscan.application.models.QuestionnaireModel;
import ch.fhnw.qualiscan.application.models.SurveyModel;
import ch.fhnw.qualiscan.application.views.AdminQuestionnaireView;
import ch.fhnw.qualiscan.core.Controller;
import ch.fhnw.qualiscan.core.db.helpers.adminDomainTableArgs;
import ch.fhnw.qualiscan.core.db.pojos.Domain;
import ch.fhnw.qualiscan.core.db.pojos.DomainText;
import ch.fhnw.qualiscan.core.db.pojos.Questionnaire;
import ch.fhnw.qualiscan.core.helpers.Permissions;
import ch.fhnw.qualiscan.core.libraries.LanguageLibrary;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.NativeSelect;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 *
 * @author bengro
 */
public class AdminQuestionnaire extends Controller {

    private SurveyModel surveyModel;
    private DomainModel domainModel;
    private AdminModel adminModel;    
    private QuestionnaireModel qModel;
    private QuestionModel questionModel;
    
    private Questionnaire currentQuestionnaire;
    
    BeanItemContainer<adminDomainTableArgs> domainsTableContainer;
    
    @Override
    public void init() {
        int questionnaireId = Integer.parseInt(getAppData().getLoaderLib().getUrlFragment(2));
        this.currentQuestionnaire = getqModel().getQuestionnaire(questionnaireId);
    }

    @Override
    public void setView() {
        view = new AdminQuestionnaireView(this);
        view.buildLayout();
    }

    @Override
    public void loadModels() {
        surveyModel = new SurveyModel();
	domainModel = new DomainModel();
        adminModel = new AdminModel();
        qModel = new QuestionnaireModel();
        questionModel = new QuestionModel();
    }

    @Override
    public void setNeededPermissions() {
        neededPermissions.add(Permissions.LOGGEDIN);
       // neededPermissions.add(Permissions.ADMINISTRATOR);
    }

    @Override
    public void urlFragmentChanged(int fragmentId, String fragment) {
        
    }
    
    public BeanItemContainer<adminDomainTableArgs> loadDomains() {
        BeanItemContainer<adminDomainTableArgs> tableContainer = new BeanItemContainer<adminDomainTableArgs>(adminDomainTableArgs.class);

        Set<Domain> domainList = currentQuestionnaire.getDomains();
        
        for(Domain domain : domainList) {
                adminDomainTableArgs adta = new adminDomainTableArgs();
                adta.setDomain(domain);
                adta.setNumberQuestions(domain.getQuestions().size());
                adta.setTitleEng(getAdminModel().getDomainText(domain, new LanguageLibrary().getDefaultLanguage()).getText());

                tableContainer.addBean(adta);
        }
        
        return tableContainer;
        
    }

    public NativeSelect getDomainSelect() {
         // load domains... here we make sure we have the latest version of the questionnaire by reloading.
        currentQuestionnaire = getqModel().getQuestionnaire(currentQuestionnaire.getId());
        Set<Domain> domainList = currentQuestionnaire.getDomains();
        
        // build index
        List<Integer> answers = new ArrayList<Integer>();
        for(Domain o : domainList) {
            answers.add(o.getId());
        }
        
        // native select
        final NativeSelect domainSelect = new NativeSelect(null, answers);
        domainSelect.setNullSelectionAllowed(false);
        domainSelect.setImmediate(true);
       
        for(Domain o : domainList) {
            DomainText dt = domainModel.getDomainText(o, userSession.getLanguage());
            domainSelect.setItemCaption(o.getId(), dt.getText());
        }
        
        return domainSelect;
        
    }
    
    /**
     * @return the surveyModel
     */
    public SurveyModel getSurveyModel() {
        return surveyModel;
    }

    /**
     * @return the domainModel
     */
    public DomainModel getDomainModel() {
        return domainModel;
    }

    /**
     * @return the adminModel
     */
    public AdminModel getAdminModel() {
        return adminModel;
    }

    /**
     * @return the qModel
     */
    public QuestionnaireModel getqModel() {
        return qModel;
    }

    /**
     * @return the questionModel
     */
    public QuestionModel getQuestionModel() {
        return questionModel;
    }

    /**
     * @return the currentQuestionnaire
     */
    public Questionnaire getCurrentQuestionnaire() {
        return currentQuestionnaire;
    }

    public void backToPanel() {
    	if (userSession.getRole().getId() == Config.ADMINISTRATOR_ID) {
    		super.getAppData().getLoaderLib().setUrl("AdminPanel/0/");
    	}
    	else {
    		super.getAppData().getLoaderLib().setUrl("Dashboard/2/");
    	}
        
    }

    
}
